//
//  DISACenterViewController.m
//  Distributor
//
//  Created by 主事丫环 on 2019/5/5.
//  Copyright © 2019 主事丫环. All rights reserved.
//

#import "DISACenterViewController.h"
#import "CSMyInfoViewController.h"
#import "DISAComplTitiController.h"
#import "CSPersonModel.h"
#import "DISAordergoodViewController.h"
#import "AinvitiveDisViewController.h"
#import "BBuildingShopViewController.h"
#import "BaccListViewController.h"
#import "MyacontViewController.h"
#import "AinvitiveWebViewController.h"

#import "ZTimeOutOrderVC.h"//超时未接单

@interface DISACenterViewController ()<UIScrollViewDelegate,UITextFieldDelegate>
@property(nonatomic,strong)UIScrollView *scrollView;
@property(nonatomic,strong)CSPersonModel *model;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *myheider;
@property(nonatomic,strong)UILabel *nameLB;
@property(nonatomic,strong)UILabel *noLable;
@property(nonatomic,strong)UILabel *yelable;
@property(nonatomic,strong)UIImageView *myheader;

@end

@implementation DISACenterViewController

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:YES animated:NO];
    //状态栏字体白色
    [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleLightContent;
    if (self.model) {
        [self request];
    }
}

- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [self.navigationController setNavigationBarHidden:NO animated:YES];
    //状态栏字体黑色
    [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleDefault;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"我的";
    self.myheider.constant = kNaviHeight;
    self.model = [[CSPersonModel alloc]init];
    [self requestcount];
    
    [self shwoui];
}

-(void)request{
    kWeakSelf;
    [NetWorkTool POST:CcentenInfo param:nil success:^(id dic) {
        
        [self.model setValuesForKeysWithDictionary:[dic objectForKey:@"data"]];
        self.nameLB.text = self.model.name;
        if (weakSelf.model.image_url.length <= 0) {
            self.myheader.image = [UIImage imageNamed:@"headerIMage"];
        } else {
            if ([self.model.image_url hasPrefix:@"http"]) {
                [self.myheader sd_setImageWithURL:[NSURL URLWithString:weakSelf.model.image_url] placeholderImage:[UIImage imageNamed:@"headerIMage"]];
            } else {
                sdimg(self.myheader,self.model.image_url);
            }
        }
    } other:^(id dic) {
        ShowToastWithText(dic[@"msg"]);
    } fail:^(NSError *error) {
        
    } needUser:YES];
}

-(void)requestcount{
    [NetWorkTool POST:complaintCount param:nil success:^(id dic) {
        self.noLable.text = [NSString stringWithFormat:@"%@",[[dic objectForKey:@"data"] objectForKey:@"countNo"]];
        self.yelable.text = [NSString stringWithFormat:@"%@",[[dic objectForKey:@"data"] objectForKey:@"countYes"]];
    } other:^(id dic) {
        ShowToastWithText(dic[@"msg"]);
    } fail:^(NSError *error) {
        
    } needUser:YES];
}
#pragma mark - 界面布局
-(void)shwoui{
    [self.view addSubview: self.scrollView];
    UIImageView *headerimage = [[UIImageView alloc] initWithFrame:CGRectMake(16, 16, 40, 40)];
    headerimage.layer.masksToBounds = YES;
    headerimage.layer.cornerRadius = 20;
    [self.scrollView addSubview:headerimage];
    
    self.myheader = headerimage;
    
    UILabel *nameLb = [[UILabel alloc]initWithFrame:CGRectMake(70, 13, kScreen_Width - 140, 22)];
    nameLb.font = [UIFont boldSystemFontOfSize:16];
    nameLb.textColor = kColorWithHex(0x666666);
    [self.scrollView addSubview:nameLb];
    self.nameLB = nameLb;
    
    UILabel *woshiAji = [[UILabel alloc]initWithFrame:CGRectMake(70, 44, 100, 19)];
    woshiAji.font = FontSize(12);
    woshiAji.textAlignment = NSTextAlignmentCenter;
    woshiAji.textColor = [UIColor colorWithHexString:@"#FAF6DD"];
    woshiAji.backgroundColor = [UIColor colorWithHexString:@"#ED9300"];
    woshiAji.layer.masksToBounds = YES;
    woshiAji.layer.cornerRadius = 10;
    woshiAji.text = @"我是省级会长";
    [self.scrollView addSubview:woshiAji];
    
    UIImageView *jinruImg = [[UIImageView alloc] initWithFrame:CGRectMake(KWIDTH-33, 21, 30, 30)];
    [jinruImg setImage:imgname(@"Jinru")];
    [self.scrollView addSubview:jinruImg];
    
    UIButton *jinrubut = [UIButton buttonWithType:(UIButtonTypeCustom)];
    jinrubut.frame = CGRectMake(0, 0, KWIDTH, 80);
    jinrubut.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0];
    [jinrubut addTarget:self action:@selector(jinquaction:) forControlEvents:(UIControlEventTouchUpInside)];
    [self.scrollView addSubview:jinrubut];
    
    UILabel *leftupLb = [[UILabel alloc]initWithFrame:CGRectMake(0, 98, KWIDTH/2, 20)];
    leftupLb.font = FontSize(14);
    leftupLb.textAlignment = NSTextAlignmentCenter;
    leftupLb.textColor = [UIColor colorWithHexString:@"#70BE68"];
    leftupLb.text = @"未处理投诉";
    [self.scrollView addSubview:leftupLb];
    
    UILabel *rightupLb = [[UILabel alloc]initWithFrame:CGRectMake(KWIDTH/2, 98, KWIDTH/2, 20)];
    rightupLb.font = FontSize(14);
    rightupLb.textAlignment = NSTextAlignmentCenter;
    rightupLb.textColor = [UIColor colorWithHexString:@"#70BE68"];
    rightupLb.text = @"处理中投诉";
    [self.scrollView addSubview:rightupLb];
    
    UILabel *leftupnumberLb = [[UILabel alloc]initWithFrame:CGRectMake(0, 118, KWIDTH/2, 24)];
    leftupnumberLb.font = FontSize(17);
    leftupnumberLb.textAlignment = NSTextAlignmentCenter;
    leftupnumberLb.textColor = [UIColor colorWithHexString:@"#70BE68"];
    leftupnumberLb.text = @"0";
    [self.scrollView addSubview:leftupnumberLb];
    self.noLable = leftupnumberLb;
    
    UILabel *rightupnumberLb = [[UILabel alloc]initWithFrame:CGRectMake(KWIDTH/2, 118, KWIDTH/2, 24)];
    rightupnumberLb.font = FontSize(17);
    rightupnumberLb.textAlignment = NSTextAlignmentCenter;
    rightupnumberLb.textColor = [UIColor colorWithHexString:@"#70BE68"];
    rightupnumberLb.text = @"0";
    [self.scrollView addSubview:rightupnumberLb];
    self.yelable =rightupnumberLb;
    
    UIButton *lefttousu = [UIButton buttonWithType:(UIButtonTypeCustom)];
    lefttousu.frame = CGRectMake(30, 90, KWIDTH/2-45, 56);
    [lefttousu addTarget:self action:@selector(lefttousuaction:) forControlEvents:(UIControlEventTouchUpInside)];
    [self.scrollView addSubview:lefttousu];
    
    UIButton *righttousu = [UIButton buttonWithType:(UIButtonTypeCustom)];
    righttousu.frame = CGRectMake(KWIDTH/2+15, 90, KWIDTH/2-45, 56);
    [righttousu addTarget:self action:@selector(righttousutousuaction:) forControlEvents:(UIControlEventTouchUpInside)];
    [self.scrollView addSubview:righttousu];
    
    UILabel *bg = [[UILabel alloc]initWithFrame:CGRectMake(0, 146, KWIDTH, 12)];
    bg.backgroundColor = [UIColor colorWithHexString:@"#F8F8F8"];
    [self.scrollView addSubview:bg];
    self.scrollView.contentSize  =  CGSizeMake(KWIDTH, bg.bottom);
    
    //NSArray *imagearr = @[@"tousuguanli",@"woyaodinghuo",@"yaoqingjiaru",@"jiesunajilu",@"wodezhanghu",@"jingqingqidia"];
    //NSArray *titlearr = @[@"投诉管理",@"我要订货",@"邀请加入",@"结算记录",@"我的账户",@"敬请期待"];
    
    NSArray *imagearr = @[@"tousuguanli",@"yaoqingjiaru",@"wodezhanghu",@"jiesunajilu",@"chaoshiweijiedan",@"buzhujinshenhe"];
    NSArray *titlearr = @[@"投诉管理",@"邀请加入",@"我的账户",@"结算记录",@"超时未接单",@"补助金审核"];
    
    for (int i = 0; i<imagearr.count; i++) {
        
        CGFloat  xxx = i%2 == 0 ?0:KWIDTH/2;
        CGFloat  yyy = i/2 *128+bg.bottom;
        UIButton *mybut = [UIButton buttonWithType:(UIButtonTypeCustom)];
        mybut.frame = CGRectMake(xxx, yyy, KWIDTH/2, 128);
        [mybut setImage:imgname(imagearr[i]) forState:(UIControlStateNormal)];
        [mybut setTitle:titlearr[i] forState:(UIControlStateNormal)];
        [mybut setTitleColor:[UIColor colorWithHexString:@"#333333"] forState:(UIControlStateNormal)];
        [self.scrollView addSubview:mybut];
        [self initButton:mybut];
        
        if (xxx>0 && i != 1) {
            UILabel *heng = [[UILabel alloc]initWithFrame:CGRectMake(20, yyy, KWIDTH-40, 1)];
            heng.backgroundColor = [UIColor colorWithHexString:@"#E5E5E5"];
            [self.scrollView addSubview:heng];
        }
        mybut.tag = 9527+i;
        [mybut addTarget:self action:@selector(mhyaction:) forControlEvents:(UIControlEventTouchUpInside)];
        self.scrollView.contentSize  =  CGSizeMake(KWIDTH, mybut.bottom);
        
        if (i == imagearr.count - 1) {
            mybut.hidden = YES;
        }
    }
    
    UILabel *shu = [[UILabel alloc]initWithFrame:CGRectMake(KWIDTH/2-0.5, 184, 1 ,332)];
    shu.backgroundColor = [UIColor colorWithHexString:@"#E5E5E5"];
    [self.scrollView addSubview:shu];
    [self request];
}

-(void)mhyaction:(UIButton *)but{
    NSInteger ind = but.tag-9527;
    
    switch (ind) {
        case 0:
        {
            //投诉管理
            KMyLog(@"投诉管理");
            DISAComplTitiController *conplitiVc = [[DISAComplTitiController alloc]init];
            conplitiVc.seleInd = 0;
            [self.navigationController pushViewController:conplitiVc animated:YES];
        }
            break;
        case 1:
        {
            //邀请加入
            KMyLog(@"邀请加入");
            [self.navigationController pushViewController:[AinvitiveWebViewController new] animated:YES];
        }
            break;
        case 2:
        {
            //我的账户
            MyacontViewController *zaac = [[MyacontViewController alloc]init];
            zaac.model = _model;
            [self.navigationController pushViewController:zaac animated:YES];
        }
            break;
        case 3:
        {
            //结算记录
            [self.navigationController pushViewController:[BaccListViewController new] animated:YES];
        }
            break;
        case 4:
        {
            //超时未接单
            ZTimeOutOrderVC *timeVC = [[ZTimeOutOrderVC alloc] init];
            [self.navigationController pushViewController:timeVC animated:YES];
        }
            break;
        default:
            break;
    }
}

-(void)initButton:(UIButton*)btn{
    //使图片和文字水平居中显示
    btn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
    //文字距离上边框的距离增加imageView的高度，距离左边框减少imageView的宽度，距离下边框和右边框距离不变
    [btn setTitleEdgeInsets:UIEdgeInsetsMake(btn.imageView.frame.size.height + 20 ,-btn.imageView.frame.size.width, 15,0.0)];
    //图片距离右边框距离减少图片的宽度，其它不边
    [btn setImageEdgeInsets:UIEdgeInsetsMake(5, 0.0,10, -btn.titleLabel.bounds.size.width)];
}

-(void)lefttousuaction:(UIButton *)but{
    //未处理投诉
    DISAComplTitiController *conplitiVc = [[DISAComplTitiController alloc]init];
    conplitiVc.seleInd = 1;
    [self.navigationController pushViewController:conplitiVc animated:YES];
}

-(void)righttousutousuaction:(UIButton *)but{
    KMyLog(@"处理中投诉");
    DISAComplTitiController *conplitiVc = [[DISAComplTitiController alloc]init];
    conplitiVc.seleInd = 2;
    [self.navigationController pushViewController:conplitiVc animated:YES];
}

-(void)jinquaction:(UIButton *)but{
    [self.navigationController pushViewController:[CSMyInfoViewController new] animated:YES];
}

- (UIScrollView *)scrollView {
    if (!_scrollView) {
        _scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, kNaviHeight, KWIDTH, KHEIGHT-kNaviHeight-kTabbarHeight)];
        _scrollView.backgroundColor = [UIColor whiteColor];
        _scrollView.delegate = self;
        adjustInset(_scrollView);
    }
    return _scrollView;
}
/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
